package com.ygqh.baby.service.task.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.SubscribeType;
import com.ygqh.baby.ao.TaskMethod;
import com.ygqh.baby.model.YgProduct4ListModel;
import com.ygqh.baby.po.YgSubscribeMsgDetail;
import com.ygqh.baby.service.WxMpMessageService;
import com.ygqh.baby.service.YgProductSecondService;
import com.ygqh.baby.service.YgSubscribeMsgDetailService;
import com.ygqh.baby.service.YgTaskTimeService;
import com.ygqh.baby.service.YgTaskTimeTemplate;
import com.ygqh.baby.service.task.YgSubscribeTaskService;
import com.ygqh.baby.utils.ExceptionUtil;

@Service
public class YgSubscribeTaskServiceImpl implements YgSubscribeTaskService {
	protected final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private YgTaskTimeService ygTaskTimeService;
	@Autowired
	private WxMpMessageService wxMpMessageService;
	@Autowired
	private YgSubscribeMsgDetailService ygSubscribeMsgDetailService;
	@Autowired
	private YgProductSecondService ygProductSecondService;
	
	@Override
	public void sendSubscribeMsg() {
		TaskMethod task = TaskMethod.SendSubscribeMsg;
		ygTaskTimeService.execute(task, task.getCode(), new YgTaskTimeTemplate() {
			
			@Override
			public void execute(Date date) throws Exception {
				System.out.println("秒杀活动商品提示任务启动");
				List<YgProduct4ListModel> productList = ygProductSecondService.findBySpecifyDate(date);
				if (CollectionUtils.isEmpty(productList)) {
					return;
				}
				System.out.println("productList --> " + productList.size());
				List<Long> ids = productList.stream().map(o -> o.getId()).collect(Collectors.toList());
				System.out.println("ids --> " + ids.size());
				List<YgSubscribeMsgDetail> findByTypeAndRelationId = ygSubscribeMsgDetailService.findByTypeAndRelationId(SubscribeType.Promotion, ids, Boolean.FALSE);
				if (CollectionUtils.isEmpty(findByTypeAndRelationId)) {
					return;
				}
				System.out.println("findByTypeAndRelationId --> " + findByTypeAndRelationId.size());
				String title = "秒杀活动开始提醒";
				String url = "http://m.yiigoo.com/product/seconds.html";
				String content = "您订阅的商品即将开始秒杀，先下单先得哦";
				List<String> successList = new ArrayList<>();
				List<String> errorList = new ArrayList<>();
				findByTypeAndRelationId.stream().forEach(msg -> {
					msg.setSendTime(date);
					String remark = "";
					try {
						Message res = wxMpMessageService.sendSubscribeMsg(msg.getOpenId(), title, msg.getTemplateId(), url, msg.getScene(), content);
						if (res.getType().equals(Message.Type.success)) {
							msg.setIsSend(Boolean.TRUE);
							msg.setRemark("ok");
							successList.add(msg.getOpenId());
							return;
						} else {
							remark = res.getContent();
						}
					} catch (Exception e) {
						remark = ExceptionUtil.getExceptionMsg(e);
					}
					msg.setRemark(remark);
					errorList.add(msg.getOpenId());
				});
				ygSubscribeMsgDetailService.updateSendStatusBatch(findByTypeAndRelationId);
				logger.info("秒杀活动提醒发送成功openId集：" + successList.toString());
				logger.info("秒杀活动提醒发送失败openId集：" + errorList.toString());
			}
		});
	}
	
}
